﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace ExcelReport
{
    public class XMLHelper
    {
        public List<FormConfigContent> FormConfigContents { get; private set; }
        public List<FormConfigContent> LoadConfigs(string path)
        {
            if (string.IsNullOrWhiteSpace(path))
                return null;
            if (!File.Exists(path))
                return null;
            var root = XElement.Load(path);
            if (root != null)
            {
                FormConfigContents = root.Elements("Content").Select(item =>
                {
                    FormConfigContent cell = new FormConfigContent();
                    cell.Direction = item.Attribute("Direction") == null ? 0 : Convert.ToInt32(item.Attribute("Direction").Value);
                    cell.Expand = item.Attribute("Expand") == null ? 0 : Convert.ToInt32(item.Attribute("Expand").Value);//这里可以写成枚举类型。
                    cell.FormCell = item.Attribute("From").Value;
                    cell.FromSheet = item.Attribute("FromSheet").Value;
                    cell.TemplateFormula = item.Attribute("Template") == null ? "" : "="+item.Attribute("Template").Value;
                    cell.ToCell = item.Attribute("To").Value;
                    cell.ToSheet = item.Attribute("ToSheet").Value;
                    return cell;
                }
                  ).ToList();
            }
            return FormConfigContents;
        }
    }
}
